<?php

/**
 * 订单管理
 */

namespace App\Http\Controllers\Assess;

use App\Http\Controllers\Backend;
use App\Imports\OrderImport;
use App\Services\Assess\OrderAndCustomersOrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;

class OrderAndCustomersOrderController extends Backend
{

    /**
     * 构造函数
     * @author wuyu
     * @since 2023/4/19
     * LinkController constructor.
     */
    public function __construct()
    {

        parent::__construct();
        $this->service = new OrderAndCustomersOrderService();
    }

    /**
     * 导入Excel
     * @author 牧羊人
     * @since 2021/5/25
     */
    public function importExcel(Request $request)
    {
        try {
            $result = upload_file($request);
            if (!$result['success']) {
                return message($result['msg'], false);
            }
            // 文件路径
            $file_path = $result['data']['file_path'];
            if (!$file_path) {
                return message("文件上传失败", false);
            }
            // 文件绝对路径
            $file_path = ATTACHMENT_PATH . $file_path;
            $OrderImport = new OrderImport();
            // 导入Excel
            Excel::import($OrderImport, $file_path);
            unlink($file_path);
            if (!empty($OrderImport->message)) {
                throw new \Exception(implode('<br/>', $OrderImport->message));
            }
        } catch (\Exception $e) {
            return message("导入信息:<br/>" . $e->getMessage(), false);
        }
        return message("导入成功", true);
    }

    /**
     * @Notes:获取订单配置
     * @Interface orderConfig
     * @return null
     * @author: wuyu
     * @Time: 2023/5/12   16:13
     */
    public function orderConfig()
    {
        return $this->service->orderConfig();
    }

    /**
     * @Notes:导入易仓订单
     * @Interface importOrder
     * @return mixed
     * @author: wuyu
     * @Time: 2023/5/26   15:58
     */
    public function importOrder()
    {
        return $this->service->importOrder();
    }

}
